// Copyright 2010 - eggsy _at_ eggsylife.co.uk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package uk.co.eggsylife.async.command;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import uk.co.eggsylife.async.http.AsyncHttpClient;


public abstract class ServerCommandImpl implements ServerCommand {
	
	private AsyncHttpClient httpClient = null;
	private String serverUrl = null;
	
	public ServerCommandImpl(String serverUrl) {
		this.httpClient = AsyncHttpClient.getInstance();
		this.serverUrl = serverUrl;
	}
	
	
	/* (non-Javadoc)
	 * @see uk.co.mobile.bookditpackdit.http.ServerCommand#executeDeleteRequest(java.util.HashMap)
	 */
	@Override
	public String executeDeleteRequest(HashMap<String, List<String>> requestParams) {
		String queryString = populateQueryString(requestParams);
		HttpDelete httpDelete = new HttpDelete(serverUrl + queryString);
		return httpClient.execute(httpDelete);
	}
	
	
	/* (non-Javadoc)
	 * @see uk.co.mobile.bookditpackdit.http.ServerCommand#executeGetRequest(java.util.HashMap)
	 */
	@Override
	public String executeGetRequest(HashMap<String, List<String>> requestParams) {
		String queryString = populateQueryString(requestParams);
		HttpGet httpGet = new HttpGet(serverUrl + queryString);
		return httpClient.execute(httpGet);
	}
	
	
	/* (non-Javadoc)
	 * @see uk.co.mobile.bookditpackdit.http.ServerCommand#executePostRequest(java.util.HashMap)
	 */
	@Override
	public String executePostRequest(HashMap<String, List<String>> requestParams) {
		String queryString = populateQueryString(requestParams);
		HttpPost httpPost = new HttpPost(serverUrl + queryString);
		return httpClient.execute(httpPost);
	}
	
	
	/* (non-Javadoc)
	 * @see uk.co.mobile.bookditpackdit.http.ServerCommand#executePutRequest(java.util.HashMap)
	 */
	@Override
	public String executePutRequest(HashMap<String, List<String>> requestParams) {
		String queryString = populateQueryString(requestParams);
		HttpPut httpPut = new HttpPut(serverUrl + queryString);
		return httpClient.execute(httpPut);
	}
	
	private String populateQueryString(HashMap<String, List<String>> requestParams) {
		List<NameValuePair> qParams = new ArrayList<NameValuePair>();
		if( requestParams != null ) {
			for( String paramName : requestParams.keySet() ) {
				for( String paramValue : requestParams.get(paramName)) {
					qParams.add(new BasicNameValuePair(paramName, paramValue));
				}
			}
		}
		String queryString = "?" + URLEncodedUtils.format(qParams, "UTF-8");
		
		return queryString;
	}


	

}
